<!--
 * @Descripttion: 电子合同
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2020-09-30 10:32:19
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @LastEditTime: 2023-05-30 17:46:52
-->
<template>
  <div class="lb-contract">
    <top-nav />
    <div class="page-main">
      <lb-tips
        >系统目前对接的是法大大第三方电子签约平台，用于和平台入驻{{
          $t('action.attendantName')
        }}签订线上合作协议，如需使用需在法大大平台购买套餐后，电子合同方可生效</lb-tips
      >
      <el-form
        @submit.native.prevent
        :model="subForm"
        :rules="subFormRules"
        ref="subForm"
        label-width="120px"
        class="basic-form"
      >
        <el-form-item label="合同名称" prop="agreement_title">
          <el-input
            v-model="subForm.agreement_title"
            placeholder="请输入合同名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="合同有效期" prop="agreement_time">
          <el-input
            v-model="subForm.agreement_time"
            placeholder="请输入合同有效期"
          >
            <template slot="append">年</template>
          </el-input>
        </el-form-item>
        <el-form-item label="合同模版" prop="agreement">
          <div class="flex-warp">
            <div class="lb-file-input flex-between">
              <div :class="[{ 'c-title': subForm.agreement }]">
                {{ subForm.agreement | handleFileName }}
              </div>
              <lb-cover
                type="button"
                fileType="file"
                :fileSize="1"
                @selectedFiles="getFile($event, 'agreement')"
              ></lb-cover>
            </div>
            <lb-tool-tips>
              请上传带公章的合同模版，文件支持pdf/doc/png/jpg/jpge格式
            </lb-tool-tips>
          </div>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitFormInfo" v-preventReClick>{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    let checkAgreement = (rule, value, callback) => {
      let { agreement_type: type } = this.subForm
      if ((type === 1 && !value) || (type === 2 && value.length === 0)) {
        callback(new Error(`请选择${rule.text}`))
      } else {
        callback()
      }
    }
    return {
      navTitle: '',
      subForm: {
        agreement_title: '',
        agreement_time: '',
        agreement: '',
        agreement_type: 1
      },
      subFormRules: {
        agreement_title: { required: true, validator: this.$reg.isNotNull, text: '合同名称', reg_type: 2, trigger: 'blur' },
        agreement_time: { required: true, validator: this.$reg.isNum, text: '合同有效期', reg_type: 2, trigger: 'blur' },
        agreement_type: { required: true, type: 'number', message: '请选择', trigger: 'blur' },
        agreement: { required: true, validator: checkAgreement, input_type: 'upload', text: '合同模版', reg_type: 2, trigger: 'blur' }
      },
      regArr: {
        1: {
          reg: /\.(doc|docx|xls|xlsx|pdf)$/,
          text: 'doc/docx/xls/xlsx/pdf'
        },
        2: {
          reg: /\.(jpg|jpeg|png)$/,
          text: 'jpg/jpeg/png'
        }
      }
    }
  },
  created () {
    this.getDetail()
  },
  methods: {
    async getDetail () {
      let { code, data } = await this.$api.system.fddConfigInfo()
      if (code !== 200) return
      let { reg } = this.regArr[2]
      if (data.agreement && data.agreement.length > 0) {
        data.agreement_type = reg.test(data.agreement) ? 2 : 1
        if (data.agreement_type === 2) {
          data.agreement = [{ url: data.agreement }]
        }
      }
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
    },
    getFile (file, key) {
      let len = file.length - 1
      this.subForm[key] = file[len].url
    },
    getCover (img, key) {
      this.subForm[key] = img
    },
    changeAgreementType (e) {
      this.subForm.agreement = e === 1 ? '' : []
    },
    async submitFormInfo () {
      let subForm = JSON.parse(JSON.stringify(this.subForm))
      let formArr = ['subForm']
      let flag = true
      for (let i = 0, len = formArr.length; i < len; i++) {
        this.$refs[formArr[i]].validate(valid => {
          if (!valid) {
            flag = false
            return false
          }
        })
      }
      if (flag) {
        if (subForm.status === 1 && subForm.agreement_type === 2) {
          subForm.agreement = subForm.agreement[0].url
        }
        let { reg, text } = this.regArr[subForm.agreement_type]
        if (subForm.status === 1 && !reg.test(subForm.agreement)) {
          this.$message.error(`合同模版只支持${text}格式!`)
          return
        }
        delete subForm.agreement_type
        let { code } = await this.$api.system.fddConfigUpdate(subForm)
        if (code !== 200) return
        this.$message.success(this.$t('tips.successSub'))
      }
    }
  },
  filters: {
    handleFileName (url, type = 1) {
      let text = type === 1 && typeof (url) === 'string' && url ? url.substring(url.lastIndexOf('/') + 1) : '请上传合同模版'
      return text
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-contract {
  width: 100%;
  .el-input,
  .el-select,
  .lb-input-number,
  .el-cascader {
    width: 300px;
  }
}
</style>
